package com.videoplayer;

import com.db.DBConnect;
import com.umeng.analytics.MobclickAgent;

import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.MediaPlayer.OnCompletionListener;
import io.vov.vitamio.MediaPlayer.OnPreparedListener;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;
import android.app.Activity;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class testVideoPlayer extends Activity {

	private VideoView mVideoView;
	private MediaController mediaController;
	private String intentTitle;
	private String intentVUrl;
	private String intentImg;
	private DBConnect dbCon;
	private String[] oriVideoUrl;
	private int i = 0;
	private int current = 0;
	private int totle = 0;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		MobclickAgent.onError(this);
		oriVideoUrl = new String[getIntent().getExtras().getBundle("url")
				.getStringArray("href").length];
		if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this)) {
			return;
		}
		
		setContentView(R.layout.video_main);
		oriVideoUrl = getIntent().getExtras().getBundle("url").getStringArray(
				"href");
		intentTitle = getIntent().getExtras().getString("title");
		intentVUrl = getIntent().getExtras().getString("vUrl");
		intentImg = getIntent().getExtras().getString("img");
		current = getIntent().getExtras().getInt("current");
		totle = getIntent().getExtras().getInt("totle");
		mVideoView = (VideoView) findViewById(R.id.surface_view);
		// Uri pathUri = Uri.parse();
		mediaController = new MediaController(this);
		// Uri pathUri = this.getIntent().getData();
		mVideoView.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer arg0) {
				// TODO Auto-generated method stub
				if (oriVideoUrl.length > 1) {
					i++;
					if (i < oriVideoUrl.length) {
						current++;
						Play(Uri.parse(oriVideoUrl[i]));
						
					}
					else{
						mVideoView.stopPlayback();
						testVideoPlayer.this.finish();
					}
				}else{
					mVideoView.stopPlayback();
					testVideoPlayer.this.finish();
				}
			}
			
			
		});
			Play(Uri.parse(oriVideoUrl[0].replace("&amp;", "&")));
	//	Play(Uri.parse("http://edge.v.iask.com/97285131.hlv?KID=sina,viask&Expires=1361116800&ssig=y%2B9KvNr7P1"));
	//	Play(Uri.parse("http://182.131.9.168/videoctfs.tc.qq.com/k0110ayd6nu.p708.1.flv?sdtfrom=v2&vkey=668A41A7F7F4FD5FF2449CBF0900CDE16B12B40DE2D8E3A56B31FCF9FC3F9B3C9BF928A52B3EFE8C&level=3&platform=11&sha=086ff2a00f1d00d3cffc9c708ebf73af60166893"));
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if(null!=intentImg){
		dbCon = new DBConnect(this);
		dbCon.open();
		dbCon.insertPlayHistory(intentTitle, intentVUrl, mVideoView.getCurrentPosition()+"", mVideoView.getDuration()+"",intentImg);
		dbCon.close();
		}
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	private void Play(Uri path) {
		// if (pathUri != null)
		mVideoView.setVideoURI(path);
		// else
		// mVideoView.setVideoPath(path);
		mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);
		mVideoView.setOnPreparedListener(new OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				mVideoView.setSubShown(true);
			}
		});
		mVideoView.requestFocus();
		mVideoView.showContextMenu();
		mVideoView.setMediaController(mediaController);
		mediaController.setFileName(intentTitle+" "+current+"/"+totle);
//		mediaController.show();
	}

	private int mLayout = VideoView.VIDEO_LAYOUT_ORIGIN;

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		if (mVideoView != null)
			mVideoView.setVideoLayout(mLayout, 0);
		super.onConfigurationChanged(newConfig);
	}
}